package com.pet.behavior.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pet.behavior.mapper.ApFollowBehaviorMapper;
import com.pet.behavior.service.ApBehaviorEntryService;
import com.pet.behavior.service.ApFollowBehaviorService;
import com.pet.common.exception.LeadNewsException;
import com.pet.model.behavior.dtos.FollowBehaviorDto;
import com.pet.model.behavior.pojos.ApBehaviorEntry;
import com.pet.model.behavior.pojos.ApFollowBehavior;
import com.pet.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * APP关注行为表 服务实现类
 * </p>
 */
@Service
@Transactional
public class ApFollowBehaviorServiceImpl extends ServiceImpl<ApFollowBehaviorMapper, ApFollowBehavior> implements ApFollowBehaviorService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Override
    public void saveFollowBehavior(FollowBehaviorDto dto) {
        ApBehaviorEntry apBehaviorEntry=apBehaviorEntryService.findByUserIdOrEquipmentId(dto.getUserId(),null);
        if(apBehaviorEntry==null){
            throw new LeadNewsException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //1.查询行为实体
        ApFollowBehavior apFollowBehavior = new ApFollowBehavior();
        apFollowBehavior.setEntryId(apBehaviorEntry.getId());
        apFollowBehavior.setArticleId(dto.getArticleId());
        apFollowBehavior.setFollowId(dto.getFollowId());
        apFollowBehavior.setCreatedTime(new Date());
        //2.添加用户关注行为数据
        save(apFollowBehavior);
    }
}
